package com.example.ycdyus.dailyfruit.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.ycdyus.dailyfruit.R;
import com.example.ycdyus.dailyfruit.bean.DemoItem;
import com.example.ycdyus.dailyfruit.utils.ImageLoaderHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;


/**
 * Sample adapter implementation extending from AsymmetricGridViewAdapter<DemoItem> This is the
 * easiest way to get started.
 */
public class DefaultListAdapter extends ArrayAdapter<DemoItem> implements DemoAdapter {

  private final LayoutInflater layoutInflater;
  private DisplayImageOptions options;

  public DefaultListAdapter(Context context, List<DemoItem> items) {
    super(context, 0, items);
    layoutInflater = LayoutInflater.from(context);
    options = ImageLoaderHelper.getDisplayImageLoader();
  }

  public DefaultListAdapter(Context context) {
    super(context, 0);
    layoutInflater = LayoutInflater.from(context);
    options = ImageLoaderHelper.getDisplayImageLoader();
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    DemoItem item = getItem(position);
    if (convertView == null) {
      v = layoutInflater.inflate(
          R.layout.item_home_mylistview, parent, false);
    } else {
      v = convertView;
    }
    ImageView iv = (ImageView) v.findViewById(R.id.iv_item_home_mylistview);
    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
    iv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));
    ImageLoader.getInstance().displayImage(item.getPath(),iv,options);
    return v;
  }

  @Override public int getViewTypeCount() {
    return 2;
  }

  @Override public int getItemViewType(int position) {
    return position % 2 == 0 ? 1 : 0;
  }

  public void appendItems(List<DemoItem> newItems) {
    addAll(newItems);
    notifyDataSetChanged();
  }

  public void setItems(List<DemoItem> moreItems) {
    clear();
    appendItems(moreItems);
  }
}